const Mock = require('mockjs')
const { find: findShopMovie, list: listShopMovie } = require('./shop-movie')
const { find: findOrgUnit } = require('./org-units')
const { findUserById } = require('./user')

const shopMovies = listShopMovie(1, 50).data.records
let Carpools = []
let id = 1
for (let i = 0; i < 26; ++i) {
  Carpools.push(Mock.mock({
    id: id++,
    title: '车次[' + id + ']准备发车',
    orgUnitId: shopMovies[i % shopMovies.length].orgUnitId,
    shopMovieId: shopMovies[i % shopMovies.length].id,
    tag: shopMovies[i % shopMovies.length].tag,
    playerCount: (i % 8) + 3,
    joinedPlayerCount: Mock.Random.integer(1, 3),
    startTime: Mock.Random.datetime(),
    status: Mock.Random.integer(1, 2),
    version: 1,
    beginTime: Mock.Random.datetime(),
    beginnerId: Mock.Random.integer(1, 15),
    terminateTime: Mock.Random.datetime(),
    terminatorId: i % 4 === 0 ? -1 : Mock.Random.integer(1, 15),
    confirmorId: Mock.Random.integer(1, 15),
    confirmTime: Mock.Random.datetime()
  }))
}

function list(page, limit, orgUnitId, shopMovieId, status, start, end) {
  limit = Math.max(1, Math.min(parseInt(limit), 50))
  page = Math.max(parseInt(page), 1)
  orgUnitId = orgUnitId ? parseInt(orgUnitId) : null
  shopMovieId = shopMovieId ? parseInt(shopMovieId) : null
  status = status ? parseInt(status) : null
  let mocks = Carpools.filter(item => {
    if (orgUnitId && item.orgUnitId !== orgUnitId) return false
    if (shopMovieId && item.shopMovieId !== shopMovieId) return false
    if (status && item.status !== status) return false
    return true
  })
  const total = mocks.length
  mocks = mocks.filter((u, index) => index < limit * page && index >= limit * (page - 1))
  mocks = mocks.map(item => {
    const orgUnit = findOrgUnit(item.orgUnitId)
    if (orgUnit) {
      item.orgUnitName = orgUnit.name
    }
    const shopMovie = findShopMovie(item.shopMovieId)
    if (shopMovie) {
      item.movieId = shopMovie.movieId
      item.movieName = shopMovie.movieName
      item.movieVersion = shopMovie.movieVersion
    }
    let u = findUserById(item.beginnerId)
    if (u) {
      item.beginnerNickname = u.nickname
      item.beginnerPhoneNumber = u.phoneNumber.substr(0, 3) + '****' + u.phoneNumber.substr(7, 4)
    }
    u = findUserById(item.terminatorId)
    if (u) {
      item.terminatorNickname = u.nickname
      item.terminatorPhoneNumber = u.phoneNumber.substr(0, 3) + '****' + u.phoneNumber.substr(7, 4)
    } else if (item.terminatorId === -1) {
      item.terminatorNickname = '系统'
    }
    u = findUserById(item.confirmorId)
    if (u) {
      item.confirmorNickname = u.nickname
      item.confirmorPhoneNumber = u.phoneNumber.substr(0, 3) + '****' + u.phoneNumber.substr(7, 4)
    }
    return item
  })
  return { success: true, data: { total: total, records: mocks }}
}

function find(id) {
  id = id ? parseInt(id) : null
  for (let i = 0; i < Carpools.length; ++i) {
    if (Carpools[i].id === id) {
      return Carpools[i]
    }
  }
  return null
}

function add(item) {
  item = JSON.parse(JSON.stringify(item))
  item.id = Carpools[Carpools.length - 1].id + 1
  item.joinedPlayerCount = 0
  Carpools.push(item)
}

function deleteRecord(id) {
  id = id ? parseInt(id) : null
  if (id) {
    Carpools = Carpools.filter(item => item.id !== id)
  }
}

function carpoolReport(report) {
  const data = JSON.parse(JSON.stringify(report))
  const mocks = Carpools.filter(item => {
    if (data.orgUnitId && item.orgUnitId !== data.orgUnitId) return false
    if (data.status && item.status !== data.status) return false
    if (data.confirmed === 0 && item.confirmorId > 0) return false
    if (data.confirmed === 1 && item.confirmorId <= 0) return false
    return true
  })
  const records = []
  for (let i = 0; i < mocks.length; ++i) {
    let record = null
    for (let j = 0; j < records.length; ++j) {
      if (records[j].orgUnitId === mocks[i].orgUnitId && records[j].userId === mocks[i].beginnerId) {
        record = records[j]
        break
      }
    }
    if (record) {
      record.count++
    } else {
      records.push({
        orgUnitId: mocks[i].orgUnitId,
        userId: mocks[i].beginnerId,
        count: 1
      })
    }
  }
  records.forEach(item => {
    const orgUnit = findOrgUnit(item.orgUnitId)
    if (orgUnit) {
      item.orgUnitName = orgUnit.name
    }
    const u = findUserById(item.userId)
    if (u) {
      item.userNickname = u.nickname
      item.userPhoneNumber = u.phoneNumber.substr(0, 3) + '****' + u.phoneNumber.substr(7, 4)
    }
  })
  data.records = records
  return data
}

module.exports = { list, find, add, deleteRecord, carpoolReport }
